#ifndef CREATE_H
#define CREATE_H

#include "syscall.h"
#include "doctorOffice.h"
#include "user.h"

/*Lock defination*/
/*1. The Lock with the Location*/
myLock waitingRoomLock = -1;
myLock examRoomLock[MAXEXAMROOMNUM];
myLock xrayRoomLock[MAXXRAYNUM];
myLock sittingRoomLock = -1;
myLock cashierRoomLock = -1;
myLock cabinetLock = -1;
myLock testLock = -1;

/*2. The Lock with service providers*/
myLock wrnLock = -1;
myLock nurseLock[MAXNURSENUM];
myLock docLock[MAXDOCNUM];
myLock cashierLock = -1;

/*3. The Lock for check state*/
myLock examStateLock = -1;
myLock docStateLock = -1;
myLock xrayStateLock = -1;

/*Condition variable defination*/
/*1. Person waiting for Room CV*/
myCV patientWaitingWRCV = -1;
myCV nurseWaitingWRCV = -1;
myCV patientWaitingCRCV = -1;
myCV nurseWaitingcabinetCV = -1;

/*2. Person waiting service providers at some Location*/
myCV patientWaitingNurseSRCV = -1;
myCV nurseWaitingDocCV = -1;
myCV patientWaitingNurseERCV[MAXEXAMROOMNUM];
myCV patientWaitingDocCV[MAXEXAMROOMNUM];
myCV patientWaitingNurseXRCV[MAXXRAYNUM];

/*3. Person interaction CV */
myCV wrnPatient_NurseCV = -1;
myCV nursePatientCV[MAXNURSENUM];
myCV docPatientCV[MAXDOCNUM];
myCV nurseXrayCV[MAXXRAYNUM];
myCV xrayPatientCV[MAXXRAYNUM];
myCV nurseDocCV[MAXDOCNUM];
myCV cashierPatientCV = -1;


/*Moniter variable*/
/*1. State*/
myMV wrnState = -1;
myMV cashierState = -1;
myMV cabinetState = -1;
myMV examState = -1;
myMV docState = -1;
myMV xrayState = -1;

/*2. Counter*/
myMV waitingRoomPatientCounter=-1;
myMV waitingRoomNurseCounter=-1;
myMV sittingRoomCounter=-1;
myMV docRoomCounter=-1;
myMV cashierRoomCounter=-1;
myMV cabinetCounter=-1;

myMV wrnTask = -1;
myMV patientORParent = -1;

/*The doctor id which response the nurse*/
myMV doc_NurseID = -1;
myMV nurseIDTOPatientSR = -1;
myMV patientIDTONurse = -1;
myMV parentIDTONurse = -1;
myMV nurseIDTOWRN = -1;
myMV docIDTOPatient = -1;
myMV examIDTODoctor = -1;
myMV examIDTOPatient = -1;
myMV nurseIDTOXray = -1;
myMV docIDTONurse = -1;
myMV nurseIDTOPatientER = -1;
myMV xrayIDTOPatient = -1;
myMV nurseIDTOPatientXR = -1;
myMV nurseIDVerify = -1;

/*test by tang*/
myMV verifyPatientID = -1;
myMV nurseSheetIDTOPatient = -1;

/* NurseID List*/
myMV nurseIDList = -1;
myMV nurseIDListNum = -1;
myMV nurseIDFirstItem = -1;
myMV nurseIDLastItem = -1;

/*examSheet exchange*/
myMV wrnExamSheet = -1;
myMV cashierExamSheet = -1;
myMV wrnExamSheetEmpty = -1;
myMV wallPocketER[MAXEXAMROOMNUM];
myMV xrayExamSheet[MAXXRAYNUM];
myMV wallPocketXR[MAXXRAYNUM];
myMV docExamSheet[MAXDOCNUM];

/*Deal with the index*/
myMV patientIndex = -1;
myMV nurseIndex = -1;
myMV doctorIndex = -1;
myMV xrayIndex = -1;

myMV patientLeftNum = -1; 
myMV patientNum = -1;
myMV examRoomNum = -1;
myMV docNum = -1;
myMV xrayRoomNum = -1;

myMV bill = -1;
/*Relative index lock*/
myLock patientIndexLock = -1;
myLock nurseIndexLock = -1;
myLock doctorIndexLock = -1;
myLock xrayIndexLock = -1;
myLock patientLeftNumLock = -1;

void rpcSetStatus(myMV mvIndex, int status) {
	rpcSetValueMV(mvIndex,9,status);
}

int rpcGetStatus(myMV mvIndex) {
	return rpcGetValueMV(mvIndex,9);
}


/*Replacement:
waiting = W
Room = R
Lock = L
exam = E
xray = X
sitting = S
cashier = C
cabinet = ca
test = T
nurse = N
doc = D ; doctor = D
state = st
patient = P
Index = I
Number = nb
Couter = ctr
Item = Im
Empty = Ey
*/



void doCreateLock() {
	/*1. The Lock with the Location*/
	waitingRoomLock = rpcCreateLock("WRL", sizeof("WRL"));
	
	/*MAXEXAMROOMNUM = 5*/
	examRoomLock[0] = rpcCreateLock("ERL0",sizeof("ERL0"));
	examRoomLock[1] = rpcCreateLock("ERL1",sizeof("ERL1"));
	examRoomLock[2] = rpcCreateLock("ERL2",sizeof("ERL2"));
	examRoomLock[3] = rpcCreateLock("ERL3",sizeof("ERL3"));
	examRoomLock[4] = rpcCreateLock("ERL4",sizeof("ERL4"));
	
	/*MAXXRAYNUM = 2*/
	xrayRoomLock[0] = rpcCreateLock("XRL0",sizeof("XRL0"));
	xrayRoomLock[1] = rpcCreateLock("XRL1",sizeof("XRL1"));
	
	sittingRoomLock = rpcCreateLock("SRL", sizeof("SRL"));
	cashierRoomLock = rpcCreateLock("CRL", sizeof("CRL"));
	cabinetLock = rpcCreateLock("caL", sizeof("caL"));	
	testLock = rpcCreateLock("TL", sizeof("TL"));
	
	/*2. The Lock with service providers*/
	wrnLock = rpcCreateLock("wrnLock", sizeof("wrnLock"));
	
	/*MAXNURSENUM = 5*/
	nurseLock[0] = rpcCreateLock("NL0",sizeof("NL0"));
	nurseLock[1] = rpcCreateLock("NL1",sizeof("NL1"));
	nurseLock[2] = rpcCreateLock("NL2",sizeof("NL2"));
	nurseLock[3] = rpcCreateLock("NL3",sizeof("NL3"));
	nurseLock[4] = rpcCreateLock("NL4",sizeof("NL4"));
	
	/*MAXDOCNUM = 3*/
	docLock[0] = rpcCreateLock("DL0",sizeof("DL0"));
	docLock[1] = rpcCreateLock("DL1",sizeof("DL1"));
	docLock[2] = rpcCreateLock("DL2",sizeof("DL2"));
	
	cashierLock = rpcCreateLock("CL", sizeof("CL"));

	/*3. The Lock for check state*/
	examStateLock = rpcCreateLock("EStL", sizeof("EStL"));
	docStateLock = rpcCreateLock("DStL", sizeof("DStL"));
	xrayStateLock = rpcCreateLock("XStL", sizeof("XStL"));
	
	/*4. Relative index lock*/
	patientIndexLock = rpcCreateLock("PIL", sizeof("PIL"));
	nurseIndexLock = rpcCreateLock("NIL", sizeof("NIL"));
	doctorIndexLock = rpcCreateLock("DIL", sizeof("DIL"));
	xrayIndexLock = rpcCreateLock("XIL", sizeof("XIL"));
	
	patientLeftNumLock = rpcCreateLock("PLeftnb", sizeof("PLeftnb"));

}

void doCreateCV() {
	/*1. Person waiting for Room CV*/
	patientWaitingWRCV=rpcCreateCondition("PWWRCV", sizeof("PWWRCV"));
	nurseWaitingWRCV=rpcCreateCondition("NWWRCV", sizeof("NWWRCV"));
	patientWaitingCRCV=rpcCreateCondition("PWCRCV", sizeof("PWCRCV"));
	nurseWaitingcabinetCV=rpcCreateCondition("NWcaCV", sizeof("NWcaCV"));

	/*2. Person waiting service providers at some Location*/
	patientWaitingNurseSRCV=rpcCreateCondition("PWNSRCV", sizeof("PWNSRCV"));
	nurseWaitingDocCV=rpcCreateCondition("NWDocCV", sizeof("NWDocCV"));
	
	/*MAXEXAMROOMNUM = 5*/
	patientWaitingNurseERCV[0]=rpcCreateCondition("PWNERCV0",sizeof("PWNERCV0"));
	patientWaitingNurseERCV[1]=rpcCreateCondition("PWNERCV1",sizeof("PWNERCV1"));
	patientWaitingNurseERCV[2]=rpcCreateCondition("PWNERCV2",sizeof("PWNERCV2"));
	patientWaitingNurseERCV[3]=rpcCreateCondition("PWNERCV3",sizeof("PWNERCV3"));
	patientWaitingNurseERCV[4]=rpcCreateCondition("PWNERCV4",sizeof("PWNERCV4"));
	
	/*MAXEXAMROOMNUM = 5*/
	patientWaitingDocCV[0]=rpcCreateCondition("PWDocCV0",sizeof("PWDocCV0"));
	patientWaitingDocCV[1]=rpcCreateCondition("PWDocCV1",sizeof("PWDocCV1"));
	patientWaitingDocCV[2]=rpcCreateCondition("PWDocCV2",sizeof("PWDocCV2"));
	patientWaitingDocCV[3]=rpcCreateCondition("PWDocCV3",sizeof("PWDocCV3"));
	patientWaitingDocCV[4]=rpcCreateCondition("PWDocCV4",sizeof("PWDocCV4"));

	/*MAXXRAYNUM = 2*/
	patientWaitingNurseXRCV[0]=rpcCreateCondition("PWNXRCV0",sizeof("PWNXRCV0"));
	patientWaitingNurseXRCV[1]=rpcCreateCondition("PWNXRCV1",sizeof("PWNXRCV1"));
	

	/*3. Person interaction CV */
	wrnPatient_NurseCV=rpcCreateCondition("wrnP_NCV", sizeof("wrnP_NCV"));
	
	/*MAXNURSENUM = 5*/
	nursePatientCV[0]=rpcCreateCondition("NPCV0",sizeof("NPCV0"));
	nursePatientCV[1]=rpcCreateCondition("NPCV1",sizeof("NPCV1"));
	nursePatientCV[2]=rpcCreateCondition("NPCV2",sizeof("NPCV2"));
	nursePatientCV[3]=rpcCreateCondition("NPCV3",sizeof("NPCV3"));
	nursePatientCV[4]=rpcCreateCondition("NPCV4",sizeof("NPCV4"));
	

	/*MAXDOCNUM = 3*/
	docPatientCV[0]=rpcCreateCondition("DPCV0",sizeof("DPCV0"));
	docPatientCV[1]=rpcCreateCondition("DPCV1",sizeof("DPCV1"));
	docPatientCV[2]=rpcCreateCondition("DPCV2",sizeof("DPCV2"));
	
	/*MAXXRAYNUM = 2*/
	nurseXrayCV[0]=rpcCreateCondition("NXCV0",sizeof("NXCV0"));
	nurseXrayCV[1]=rpcCreateCondition("NXCV1",sizeof("NXCV1"));

	/*MAXXRAYNUM = 2*/
	xrayPatientCV[0]=rpcCreateCondition("XPCV0",sizeof("XPCV0"));
	xrayPatientCV[1]=rpcCreateCondition("XPCV1",sizeof("XPCV1"));
	
	/*MAXDOCNUM = 3*/
	nurseDocCV[0]=rpcCreateCondition("NDCV0",sizeof("NDCV0"));
	nurseDocCV[1]=rpcCreateCondition("NDCV1",sizeof("NDCV1"));
	nurseDocCV[2]=rpcCreateCondition("NDCV2",sizeof("NDCV2"));
	
	cashierPatientCV=rpcCreateCondition("CPCV", sizeof("CPCV"));	
}

void doCreateMV() {
	/*1. State*/
	wrnState = rpcCreateMV("wrnState",sizeof("wrnState"),1,BUSY);
	cashierState = rpcCreateMV("CSt",sizeof("CSt"),1,BUSY);
	cabinetState = rpcCreateMV("caSt",sizeof("caSt"),1,FREE);
	
	examState = rpcCreateMV("ESt",sizeof("ESt"),MAXEXAMROOMNUM,FREE);
	docState = rpcCreateMV("DSt",sizeof("DSt"),MAXDOCNUM,FREE);
	xrayState = rpcCreateMV("XSt",sizeof("XSt"),MAXXRAYNUM,FREE);
	
	
	/*2. Counter*/
	waitingRoomPatientCounter = rpcCreateMV("WRPCtr",sizeof("WRPCtr"),1,0);
	waitingRoomNurseCounter = rpcCreateMV("WRNCtr",sizeof("WRNCtr"),1,0);
	sittingRoomCounter = rpcCreateMV("SRCtr",sizeof("SRCtr"),1,0);	
	docRoomCounter = rpcCreateMV("DRCtr",sizeof("DRCtr"),1,0);
	cashierRoomCounter = rpcCreateMV("CRCtr",sizeof("CRCtr"),1,0);	
	cabinetCounter = rpcCreateMV("caCtr",sizeof("caCtr"),1,0);
	wrnTask = rpcCreateMV("wrnTask",sizeof("wrnTask"),1,WRONGTASK);

	/*The doctor id which response the nurse*/
	doc_NurseID = rpcCreateMV("D_NID",sizeof("D_NID"),1,INT_MAX);
	nurseIDTOPatientSR = rpcCreateMV("NIDTOPSR",sizeof("NIDTOPSR"),1,INT_MAX);	
	patientIDTONurse = rpcCreateMV("PIDTON",sizeof("PIDTON"),1,INT_MAX);	
	/*parentIDTONurse = rpcCreateMV("parentIDTONurse",sizeof("parentIDTONurse"),1,INT_MAX);*/
	nurseIDTOWRN = rpcCreateMV("NIDTOWRN",sizeof("NIDTOWRN"),1,INT_MAX);
	docIDTOPatient = rpcCreateMV("DIDTOP",sizeof("DIDTOP"),MAXEXAMROOMNUM,INT_MAX);
	examIDTODoctor = rpcCreateMV("EIDTOD",sizeof("EIDTOD"),MAXDOCNUM,INT_MAX);	
	examIDTOPatient = rpcCreateMV("EIDTOP",sizeof("EIDTOP"),MAXNURSENUM,INT_MAX);
	nurseIDTOXray = rpcCreateMV("NIDTOX",sizeof("NIDTOX"),MAXXRAYNUM,INT_MAX);
	docIDTONurse = rpcCreateMV("DIDTON",sizeof("DIDTON"),MAXNURSENUM,INT_MAX);
	nurseIDTOPatientER = rpcCreateMV("NIDTOPER",sizeof("NIDTOPER"),MAXEXAMROOMNUM,INT_MAX);
	xrayIDTOPatient = rpcCreateMV("XIDTOP",sizeof("XIDTOP"),MAXNURSENUM,INT_MAX);	
	nurseIDTOPatientXR = rpcCreateMV("NIDTOPXR",sizeof("NIDTOPXR"),MAXXRAYNUM,INT_MAX);	
	nurseIDVerify = rpcCreateMV("NIDVerify",sizeof("NIDVerify"),MAXDOCNUM,INT_MAX);	

	/*test by tang*/
	verifyPatientID = rpcCreateMV("verifyPID",sizeof("verifyPID"),MAXEXAMROOMNUM,INT_MAX);	
	nurseSheetIDTOPatient = rpcCreateMV("NSheetIDTOP",sizeof("NSheetIDTOP"),MAXNURSENUM,INT_MAX);	


	/* NurseID List*/
	nurseIDList = rpcCreateMV("NIDList",sizeof("NIDList"),MAXPATIENTNUM,-1);
	nurseIDListNum = rpcCreateMV("NIDListNum",sizeof("NIDListNum"),1,MAXPATIENTNUM);
	nurseIDFirstItem = rpcCreateMV("NIDFirstIm",sizeof("NIDFirstIm"),1,0);
	nurseIDLastItem = rpcCreateMV("NIDLastIm",sizeof("NIDLastIm"),1,-1);	
	
	
	/*examSheet exchange*/
	wrnExamSheet = rpcCreateMV("wrnESheet",sizeof("wrnESheet"),EXAMSHEETITEM,0);	
	cashierExamSheet = rpcCreateMV("CESheet",sizeof("CESheet"),EXAMSHEETITEM,0);
	wrnExamSheetEmpty = rpcCreateMV("wrnESheetEy",sizeof("wrnESheetEy"),EXAMSHEETITEM,0);
	
	/*MAXEXAMROOMNUM = 5*/
	wallPocketER[0] = rpcCreateMV("wallPocketER0",sizeof("wallPocketER0"),EXAMSHEETITEM,0);
	wallPocketER[1] = rpcCreateMV("wallPocketER1",sizeof("wallPocketER1"),EXAMSHEETITEM,0);
	wallPocketER[2] = rpcCreateMV("wallPocketER2",sizeof("wallPocketER2"),EXAMSHEETITEM,0);
	wallPocketER[3] = rpcCreateMV("wallPocketER3",sizeof("wallPocketER3"),EXAMSHEETITEM,0);
	wallPocketER[4] = rpcCreateMV("wallPocketER4",sizeof("wallPocketER4"),EXAMSHEETITEM,0);
	
	/*MAXXRAYNUM = 2*/
	xrayExamSheet[0] = rpcCreateMV("XESheet0",sizeof("XESheet0"),EXAMSHEETITEM,0);
	xrayExamSheet[1] = rpcCreateMV("XESheet1",sizeof("XESheet1"),EXAMSHEETITEM,0);
	
	/*MAXXRAYNUM = 2*/
	wallPocketXR[0] = rpcCreateMV("wallPocketXR0",sizeof("wallPocketXR0"),EXAMSHEETITEM,0);
	wallPocketXR[1] = rpcCreateMV("wallPocketXR1",sizeof("wallPocketXR1"),EXAMSHEETITEM,0);
	
	/*MAXDOCNUM = 3*/
	docExamSheet[0] = rpcCreateMV("DESheet0",sizeof("DESheet0"),EXAMSHEETITEM,0);
	docExamSheet[1] = rpcCreateMV("DESheet1",sizeof("DESheet1"),EXAMSHEETITEM,0);
	docExamSheet[2] = rpcCreateMV("DESheet2",sizeof("DESheet2"),EXAMSHEETITEM,0);
	
	rpcSetStatus(wallPocketER[0], EMPTY);
	rpcSetStatus(wallPocketER[1], EMPTY);
	rpcSetStatus(wallPocketER[2], EMPTY);
	rpcSetStatus(wallPocketER[3], EMPTY);
	rpcSetStatus(wallPocketER[4], EMPTY);
	
	rpcSetStatus(wallPocketXR[0], EMPTY);
	rpcSetStatus(wallPocketXR[1], EMPTY);

/*	
	struct examSheet* wrnExamSheet = NULL;
	struct examSheet* cashierExamSheet = NULL;
	struct examSheet* wrnExamSheetEmpty = NULL;
	struct examSheet* wallPocketER[MAXEXAMROOMNUM];
	struct examSheet* xrayExamSheet[MAXXRAYNUM];
	struct examSheet* wallPocketXR[MAXXRAYNUM];
	struct examSheet* docExamSheet[MAXDOCNUM];*/
	
	patientIndex = rpcCreateMV("PIndex",sizeof("PIndex"),1,0);
	nurseIndex = rpcCreateMV("NIndex",sizeof("NIndex"),1,0);
	doctorIndex = rpcCreateMV("DIndex",sizeof("DIndex"),1,0);
	xrayIndex = rpcCreateMV("XIndex",sizeof("XIndex"),1,0);

	patientLeftNum = rpcCreateMV("PLeftNum",sizeof("PLeftNum"),1,0);

	patientNum = rpcCreateMV("PNum",sizeof("PNum"),1,0);
	examRoomNum = rpcCreateMV("ERNUM",sizeof("ERNUM"),1,0);
	docNum = rpcCreateMV("docNum",sizeof("docNum"),1,0);
	xrayRoomNum = rpcCreateMV("XRNum",sizeof("XRNum"),1,0);


	bill = rpcCreateMV("bill",sizeof("bill"),1,0);	
	


}

void doCreate() {
	doCreateLock();
	doCreateCV();
	doCreateMV();
}
/*
struct examSheet {
	int patientID;
	char* name;
	int age;
		
	char* symptom;
	bool needShot;
	bool needXray;
	int imageNum;
	int xrayResult;
	int nextDestination;
};
*/
void rpcSetExamSheet(myMV mvIndex, struct examSheet valueSheet) {
	rpcSetValueMV(mvIndex,0,valueSheet.patientID);
	rpcSetValueMV(mvIndex,1,(int)(valueSheet.name));
	rpcSetValueMV(mvIndex,2,valueSheet.age);
	rpcSetValueMV(mvIndex,3,(int)(valueSheet.symptom));
	rpcSetValueMV(mvIndex,4,valueSheet.needShot);
	rpcSetValueMV(mvIndex,5,valueSheet.needXray);
	rpcSetValueMV(mvIndex,6,valueSheet.imageNum);
	rpcSetValueMV(mvIndex,7,valueSheet.xrayResult);
	rpcSetValueMV(mvIndex,8,valueSheet.nextDestination);
}

void rpcGetExamSheet(myMV mvIndex, struct examSheet* aimSheet) {
	aimSheet->patientID = rpcGetValueMV(mvIndex,0);
	aimSheet->name =(char*) rpcGetValueMV(mvIndex,1);
	aimSheet->age = rpcGetValueMV(mvIndex,2);
	aimSheet->symptom = (char*)rpcGetValueMV(mvIndex,3);
	aimSheet->needShot = rpcGetValueMV(mvIndex,4);
	aimSheet->needXray = rpcGetValueMV(mvIndex,5);
	aimSheet->imageNum = rpcGetValueMV(mvIndex,6);
	aimSheet->xrayResult = rpcGetValueMV(mvIndex,7);
	aimSheet->nextDestination = rpcGetValueMV(mvIndex,8);
}




#endif